package com.ruoyi.web.controller.module.event;

import java.util.List;
import java.util.Arrays;
import com.ruoyi.web.controller.common.BaseController;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.estate.Estate;
import com.ruoyi.system.domain.module.event.EventMaintenanceCompany;
import com.ruoyi.system.domain.module.property.Property;
import com.ruoyi.system.service.module.estate.IEstateService;
import com.ruoyi.system.service.module.event.IEventMaintenanceCompanyService;
import com.ruoyi.system.service.module.property.IPropertyService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 维保商企业Controller
 *
 * @author ruoyi
 * @date 2023-12-26
 */
@Validated
@Api(value = "维保商企业控制器", tags = {"维保商企业管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/maintenance/company")
public class EventMaintenanceCompanyController extends BaseController {

    private final IEventMaintenanceCompanyService eventMaintenanceCompanyService;
	private final IPropertyService propertyService;
	private final IEstateService estateService;

    /**
     * 查询维保商企业列表
     */
    @ApiOperation("查询维保商企业列表")
    @PreAuthorize("@ss.hasPermi('module:maintenanceCompany:list')")
    @GetMapping("/list")
    public TableDataInfo<EventMaintenanceCompany> list(@Validated EventMaintenanceCompany eventMaintenanceCompany) {
        QueryWrapper<EventMaintenanceCompany> qw = new QueryWrapper<>();
		SysUser sysUser = getUser();
		if(sysUser.getUserType() == 2){
			qw.eq("property_id",sysUser.getPropertyId());
			if (sysUser.getEstateId()!=null){
				qw.eq("estate_id",sysUser.getEstateId());
			}
		} else if(sysUser.getUserType() == 3){
			qw.eq("estate_id",sysUser.getEstateId());
		}else{
			if(eventMaintenanceCompany.getPropertyId() != null){
				qw.eq("property_id",eventMaintenanceCompany.getPropertyId());
			}
			if(eventMaintenanceCompany.getEstateId() != null){
				qw.eq("estate_id",eventMaintenanceCompany.getEstateId());
			}
		}
		if (eventMaintenanceCompany.getPropertyId()!=null){
			qw.eq("property_id",eventMaintenanceCompany.getPropertyId());
		}
		if (eventMaintenanceCompany.getEstateId()!=null){
			qw.eq("estate_id",eventMaintenanceCompany.getEstateId());
		}
        TableDataInfo<EventMaintenanceCompany> td = PageUtils.buildDataInfo(eventMaintenanceCompanyService.page(PageUtils.buildPage(),qw));
        List<EventMaintenanceCompany> list = td.getRows();
		if (list.size()>0){
			for (EventMaintenanceCompany company : list){
				if (company.getPropertyId()!=null){
					Property property = propertyService.findById(company.getPropertyId());
					if (property!=null){
						company.setPropertyName(property.getPropertyName());
					}
				}
				if (company.getEstateId()!=null){
					Estate estate = estateService.findById(company.getEstateId());
					if (estate!=null){
						company.setEstateName(estate.getEstateName());
					}
				}
			}
		}
        return td;
    }

    /**
     * 导出维保商企业列表
     */
    @ApiOperation("导出维保商企业列表")
    @PreAuthorize("@ss.hasPermi('module:maintenanceCompany:export')")
    @Log(title = "维保商企业", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<EventMaintenanceCompany> export(@Validated EventMaintenanceCompany eventMaintenanceCompany) {
        List<EventMaintenanceCompany> list = eventMaintenanceCompanyService.list();
        ExcelUtil<EventMaintenanceCompany> util = new ExcelUtil<EventMaintenanceCompany>(EventMaintenanceCompany.class);
        return util.exportExcel(list, "维保商企业");
    }

    /**
     * 获取维保商企业详细信息
     */
    @ApiOperation("获取维保商企业详细信息")
    @PreAuthorize("@ss.hasPermi('module:maintenanceCompany:query')")
    @GetMapping("/{id}")
    public AjaxResult<EventMaintenanceCompany> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Integer id) {
        EventMaintenanceCompany eventMaintenanceCompany = eventMaintenanceCompanyService.getById(id);
        return AjaxResult.success(eventMaintenanceCompany);
    }

    /**
     * 新增维保商企业
     */
    @ApiOperation("新增维保商企业")
    @PreAuthorize("@ss.hasPermi('module:maintenanceCompany:add')")
    @Log(title = "维保商企业", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody EventMaintenanceCompany eventMaintenanceCompany) {
        return toAjax(eventMaintenanceCompanyService.save(eventMaintenanceCompany) ? 1 : 0);
    }

    /**
     * 修改维保商企业
     */
    @ApiOperation("修改维保商企业")
    @PreAuthorize("@ss.hasPermi('module:maintenanceCompany:edit')")
    @Log(title = "维保商企业", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody EventMaintenanceCompany eventMaintenanceCompany) {
        return toAjax(eventMaintenanceCompanyService.updateById(eventMaintenanceCompany) ? 1 : 0);
    }

    /**
     * 删除维保商企业
     */
    @ApiOperation("删除维保商企业")
    @PreAuthorize("@ss.hasPermi('module:maintenanceCompany:remove')")
    @Log(title = "维保商企业" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Integer[] ids) {
        return toAjax(eventMaintenanceCompanyService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
